library (pacman)
## Warning: package 'pacman' was built under R version 4.4.3
library (ggplot2)
library(mapdata)
## Warning: package 'mapdata' was built under R version 4.4.3
## Loading required package: maps
## Warning: package 'maps' was built under R version 4.4.3
p_load(tidyverse)

A Simple World Map

map <- map_data("world")
ggplot(map,
       aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="lightblue", colour = "white")+
  theme_void()

A Map for Specific Regions

North_Asia <- c("China", "Japan", "Mongolia",
                "North Korea", "South Korea", "Taiwan")
North_Asia_Map <- map_data("world", region = North_Asia)

region.data <- North_Asia_Map %>%
  group_by(region) %>%
  summarise(long = mean(long), lat = mean(lat)) %>%
  arrange()

ggplot(North_Asia_Map,
       aes(x=long, y=lat)) +
  geom_polygon(aes(group = group, fill = region)) +
  geom_text (data = region.data, aes(label = region),
             size = 5, hjust = 0.5, col = "#808080", 
               fontface = "bold") +
  scale_fill_viridis_d() +
  theme_void () +
  theme (legend.position = "none")

A Choropleth Map

drinks <- read_csv("./drinks.csv")
## Rows: 193 Columns: 5
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (1): country
## dbl (4): beer_servings, spirit_servings, wine_servings, total_litres_of_pure...
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
drinks_map <- drinks %>%
  left_join(map, by = c("country" = "region"))
ggplot(drinks_map,
       aes(long, lat, group = group)) +
  geom_polygon(aes(fill = total_litres_of_pure_alcohol),
               colour = "white") +
  scale_fill_viridis_c(option = "C") +
  labs(fill = "Totals Litres of Pure Alcohol") +
  theme_void() +
  theme (legend.position = "bottom")

ggplot (drinks,
        aes(map_id = country)) +
  geom_map(aes(fill = total_litres_of_pure_alcohol),
            map = map, colour = "white") +
  expand_limits(x = map$long, y = map$lat) +
  labs(fill = "Totals Litres of Pure Alcohol") +
  theme_void() +
  theme (legend.position = "bottom")

A U.S State-Level Map

US_map <- map_data("state")

state_data <- US_map %>%
  filter(region != "district of columbia") %>%
  group_by(region) %>%
  summarise (long = mean(long), lat = mean(lat)) %>%
  arrange (region)

state_data$region.abb <- state.abb[-c(2,11)] 

p <- ggplot(US_map, aes(x = long, y = lat)) +
  geom_polygon(aes(group = group, fill = region),
            colour = "white") +
  geom_text(data = state_data,
            aes(label = region.abb),
            fontface = "bold") +
  theme_void() +
  theme(legend.position = "none")

p

A Dynamic Map using plotly - 1

library (plotly)
## Warning: package 'plotly' was built under R version 4.4.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
ggplotly(p)

A Dynamic Map using plotly - 2

US_map <- map_data ("state")

state_data <- US_map %>%
  filter(region != "district of columbia") %>%
  group_by(region) %>%
  summarise(long = mean(long), lat = mean(lat)) %>%
  arrange(region)

state_data$region.abb <- state.abb[-c(2,11)]

crimes <- data.frame(region = rownames(USArrests), USArrests) %>%
  filter(region != c ("Alaska", "Hawaii"))

crimes$region <-tolower(crimes$region)

crimes_map <- crimes %>%
  left_join(US_map, by = "region")

g1 <- ggplot(crimes_map, aes(x = long, y = lat)) +
  geom_polygon(aes(group=group, fill = Murder,
                   text = paste0(region, ":/n",
                  Murder, "murder arrests per 100,000"),
                   colour = "white")) +
  geom_text(data = state_data,
            aes(label = region.abb), fontface = "bold", size=3) +
  theme_void()
## Warning in geom_polygon(aes(group = group, fill = Murder, text = paste0(region,
## : Ignoring unknown aesthetics: text
ggplotly(g1, tooltip="text")